package com.ruoyi.web.controller.farmer;

import com.agri.api.service.farmer.IFarmerProductSpecificationsService;
import com.agri.api.service.farmer.IFarmerProductsService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.model.farmer.domain.FarmerProducts;
import com.ruoyi.common.model.farmer.dto.req.FarmerProductsAddReq;
import com.ruoyi.common.model.farmer.dto.req.FarmerProductsReq;
import com.ruoyi.common.model.farmer.dto.resp.FarmerProductsCardResp;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * productsController
 *
 * @author huixh
 * @date 2025-01-05
 */
@Api
@RestController
@RequestMapping("/farmer/products")
public class FarmerProductsController extends BaseController {

    @Resource
    private IFarmerProductsService farmerProductsService;

    @Resource
    private IFarmerProductSpecificationsService farmerProductSpecificationsService;

    /**
     * 查询商品列表
     */
    @ApiOperation(value = "商品列表")
    @PreAuthorize("@ss.hasPermi('farmer:products:list')")
    @GetMapping("/list")
    public TableDataInfo list(FarmerProducts farmerProducts)
    {
        //startPage();
        List<FarmerProductsCardResp> list = farmerProductsService.selectFarmerProductsList(farmerProducts);
        return getDataTable(list);
    }


    /**
     * 导出products列表
     */
    @PreAuthorize("@ss.hasPermi('farmer:products:export')")
    @Log(title = "products", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FarmerProducts farmerProducts)
    {
        //List<FarmerProducts> list = farmerProductsService.selectFarmerProductsList(farmerProducts);
        ExcelUtil<FarmerProducts> util = new ExcelUtil<FarmerProducts>(FarmerProducts.class);
       // util.exportExcel(response, list, "products数据");
    }

    /**
     * 根据分 商品id 查询规格
     * @param id 商品id
     * @return AjaxResult
     */
    @Log(title = "products", businessType = BusinessType.OTHER)
    @ApiOperation(value = "根据商品id查询规格")
    @GetMapping("/specifications/{id}")
    public AjaxResult getProductSpecifications(@PathVariable("id") Long id)
    {
        return success(farmerProductSpecificationsService.selectFarmerProductSpecificationsList(id));
    }


    /**
     * 获取products详细信息
     */
    @PreAuthorize("@ss.hasPermi('farmer:products:query')")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult getProductDetail(@PathVariable("id") Long id)
    {
        return success(farmerProductsService.selectFarmerProductsDetailById(id));
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('farmer:products:add')")
    @Log(title = "products", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FarmerProductsAddReq farmerProducts)
    {
        farmerProductsService.insertFarmerProducts(farmerProducts);
        return toAjax(true);
    }

    /**
     * 修改products
     */
    @PreAuthorize("@ss.hasPermi('farmer:products:edit')")
    @Log(title = "products", businessType = BusinessType.UPDATE)
    @PutMapping("{id}")
    public AjaxResult edit(@PathVariable("id") Long id,@RequestBody FarmerProductsAddReq farmerProducts)
    {
        farmerProductsService.updateFarmerProducts(id,farmerProducts);
        return toAjax(true);
    }

    /**
     * 删除products
     */
    @PreAuthorize("@ss.hasPermi('farmer:products:remove')")
    @Log(title = "products", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(farmerProductsService.deleteFarmerProductsByIds(ids));
    }
}
